// error 错误提示

export function getmessage(data){
    for (var key in data)
        return data[key];
}

export function parseParam(param,key,encode){
   
      if(param==null) return '';
        var paramStr = '';
        var t = typeof (param);
        if (t == 'string' || t == 'number' || t == 'boolean') {
          paramStr += '&' + key + '=' + ((encode==null||encode) ? encodeURIComponent(param) : param);
        } else {
          for (var i in param) {
            var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
            paramStr += parseParam(param[i], k, encode);
          }
        }
        return paramStr;
}

// 时间转换  年-月-日  时：分：秒

export function getdate(timestamp,num) {
    timestamp = timestamp+'';
    timestamp = timestamp.length==10?timestamp*1000:timestamp;
    var date = new Date(timestamp);
    var y = date.getFullYear();  
    var m = date.getMonth() + 1;  
    m = m < 10 ? ('0' + m) : m;  
    var d = date.getDate();  
    d = d < 10 ? ('0' + d) : d;  
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;  
    second = second < 10 ? ('0' + second) : second; 
    if(num==0){
        return y + '-' + m + '-' + d;  
    }else if(num==1){
        return y + '-' + m + '-' + d +' '+ h +':'+ minute +':' + second;  
    }else if(num==2){
        return y + '年' + m + '月' + d +'日 '+ h +':'+ minute;  
    }
}
// 当前时间格式化
export function dateType(num) {
	var myDate = new Date(); //实例一个时间对象；
	myDate.getFullYear();   //获取系统的年；
	myDate.getMonth()+1;   //获取系统月份，由于月份是从0开始计算，所以要加1
	myDate.getDate(); // 获取系统日，
	myDate.getHours(); //获取系统时，
	myDate.getMinutes(); //分
	myDate.getSeconds(); //秒
	if(num == 1) {
		return myDate.getHours()
	}else if(num == 2) {
		return myDate.getHours() + ':' + myDate.getMinutes();
	}else if(num == 3) {
		return myDate.getHours() + ':' + myDate.getMinutes() + ':' + myDate.getSeconds();;
	}
}
// 营业时间判断
export function timeRange(beginTime, endTime, nowTime) {//判断是否在缴费查询时间
  var strb = beginTime.split(":");
  if (strb.length != 2) {
    return false;
  }
 
  var stre = endTime.split (":");
  if (stre.length != 2) {
    return false;
  }
 
  var strn = nowTime.split (":");
  if (stre.length != 2) {
    return false;
  }
  var b = new Date ();
  var e = new Date ();
  var n = new Date ();
 
  b.setHours (strb[0]);
  b.setMinutes (strb[1]);

  e.setHours (stre[0]);
  e.setMinutes (stre[1]);

  n.setHours (strn[0]);
  n.setMinutes (strn[1]);

  if (n.getTime () - b.getTime () > 0 && n.getTime () - e.getTime () < 0) {
    return true;
	
  } else {
    // alert ("当前时间是：" + n.getHours () + ":" + n.getMinutes () + ":"+n.getSeconds()+"，不在该时间范围内！");
    return false;
  }
}

// 计算当前距离
//经纬度转换成三角函数中度分表形式。
function rad(d) {
  return d * Math.PI / 180.0;
}

/**
 *
 * @param lat1  纬度1
 * @param lng1  经度1
 * @param lat2  纬度2
 * @param lng2  经度2
 */
export function geoDistance(lat1, lng1, lat2, lng2) {
  let radLat1 = rad(lat1);
  let radLat2 = rad(lat2);
  let a = radLat1 - radLat2;
  let b = rad(lng1) - rad(lng2);
  let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  s = s * 6378.137;// EARTH_RADIUS;
  s = Math.round(s * 10000) / 10000; //输出为公里
  let t = s * 1000
  return t;
}


//计算倒计时秒数
/**
 * [dateDiff 算时间差]
 * @param  {[type=Number]} hisTime [历史时间戳，必传]
 * @param  {[type=Number]} nowTime [当前时间戳，不传将获取当前时间戳]
 * @return {[string]}         [string]
 */
export function dateDiff(hisTime, nowTime) {
	var now =Math.round(new Date().getTime()/1000),
		diffValue = now - parseInt(hisTime);
		// result='',
		
		// second = 60 * 60,
		// minute = 60,
		// hour = minute * 60,
		// day = hour * 24,
		// halfamonth = day * 15,
		// month = day * 30,
		// year = month * 12,

		// _year = diffValue/year,
		// _month =diffValue/month,
		// _week =diffValue/(7*day),
		// _day =diffValue/day,
		// _hour =diffValue/hour,
		// _min =diffValue/minute,
		// _sec =diffValue/second;
		
		// console.log(diffValue)

		// if(_year>=1) result=parseInt(_year) + "年前";
		// else if(_month>=1) result=parseInt(_month) + "个月前";
		// else if(_week>=1) result=parseInt(_week) + "周前";
		// else if(_day>=1) result=parseInt(_day) +"天前";
		// else if(_hour>=1) result=parseInt(_hour) +"个小时前";
		// else if(_min>=1) result=parseInt(_min) +"分钟前";
		// else if(_sec>=1) result=parseInt(_sec) +"秒前";
		// else result="刚刚";
		return diffValue;
}
// 根据起始时间计算倒计时
export function endCountDown(enTime) {
	var now =Math.round(new Date().getTime()/1000),
	    countDown = parseInt(enTime) - now;
	return countDown;
}

// 和当前比较时间
export function compareTimeNow(time){
	var now =Math.round(new Date().getTime()/1000),
	    countDown = parseInt(time) - now;
	return countDown;
}

// 解析小程序url上带的参数
export function resolvingScene(scene){
	var sceneVal = decodeURIComponent(scene);
	var analysisScene = {};
	sceneVal.split('&').forEach((item) => {
		const key = item.split("=")[0]
		analysisScene[key] = item.split('=')[1];
	})
	return analysisScene;
}